package intf;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

public class RunWindowsCmd {
	
	class SyncPipe implements Runnable
	{
		public SyncPipe(InputStream istrm, OutputStream ostrm) {
			istrm_ = istrm;
			ostrm_ = ostrm;
		}
		public void run() {
			try {
				final byte[] buffer = new byte[1024];
				for (int length = 0; (length = istrm_.read(buffer)) != -1; ) {
					ostrm_.write(buffer, 0, length);
				}
			} catch (Exception e) {
				System.out.println("@RunWindowsCmd - Exception windowscmd: " + e.getMessage());
			}
		}
	  
		private final OutputStream ostrm_;
		private final InputStream istrm_;
	}
	
	public RunWindowsCmd(String s) throws Exception {
	String[] command =
		{
	        "cmd",
	    };
	    Process p = Runtime.getRuntime().exec(command);
	    new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
	    new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
	    PrintWriter stdin = new PrintWriter(p.getOutputStream());
	    
	    stdin.println(s);
	    stdin.close();
	    
	    int returnCode = -1;
		try {
			returnCode = p.waitFor();
		} catch (InterruptedException e) {
			System.out.println("@RunWindowsCmd - InterruptedException: " + e.getMessage());
		}
		System.out.println("@RunWindowsCmd - Return code = " + returnCode);	
	}
}
	
	